package com.csc.controller;

import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csc.bean.BlogLogonBean;
import com.csc.domain.SystemTimeUsed;
import com.csc.service.SystemTimeUsedService;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 
 * @author Nguyen Vu Hung
 * 
 */
@Controller
public class SystemTimeUsedController {
	@Autowired
	SystemTimeUsedService sysTimeSer;

	static final Logger logger = Logger.getLogger(SystemAdminController.class);

	@RequestMapping(value = "/bloglogon", method = RequestMethod.GET)
	public String goBloglogon(Model model) {
		return "admin/bloglogon";
	}

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String test(Model model) {
		return "admin/test";
	}

	// Get all history login - logout
	@RequestMapping(value = "/bloglogonjson", method = RequestMethod.GET)
	public @ResponseBody
	List<BlogLogonBean> goBlogLogonJSON() {
		List<SystemTimeUsed> listAll = sysTimeSer.getAllListSystemTimeUsed();
		return sysTimeSer.parseToBlogLogon(listAll);
	}

	// Search bloglogon by name
	@RequestMapping(value = "/searchnamebloglogon/{name}", method = RequestMethod.GET)
	public @ResponseBody
	List<BlogLogonBean> searchNameBlogLogon(@PathVariable("name") String name) {
		List<SystemTimeUsed> listAll = sysTimeSer.searchByName(name);
		return sysTimeSer.parseToBlogLogon(listAll);
	}

	// Autocomplete
	@RequestMapping(value = "/bloglogonautocomplete", method = RequestMethod.GET)
	public @ResponseBody
	HashSet<String> autoCompleteBloglogon() {
		List<SystemTimeUsed> listAll = sysTimeSer.getAllListSystemTimeUsed();
		HashSet<String> set = new HashSet<String>();

		for (SystemTimeUsed sysTime : listAll) {
			set.add(sysTime.getSysAccount().getAccountFirstName() + " "
					+ sysTime.getSysAccount().getAccountMidName() + " "
					+ sysTime.getSysAccount().getAccountLastName());
		}
		return set;
	}

	// Search advanced bloglogon
	@RequestMapping(value = "/advancedSearchTimeUsed/{username}/{cmbRole}/{datefrom}/{dateto}", method = RequestMethod.GET)
	public @ResponseBody
	List<BlogLogonBean> advancedSearchTimeUsed(
			@PathVariable("username") String username,
			@PathVariable("cmbRole") String role,
			@PathVariable("datefrom") String dateFrom,
			@PathVariable("dateto") String dateTo) {
		List<SystemTimeUsed> listAll = sysTimeSer.searchAdvancedBlogon(
				username, role, dateTo, dateFrom);
		return sysTimeSer.parseToBlogLogon(listAll);
	}

	// Print PDF
	@RequestMapping(value = "/generatePDFBlogLogon", method = RequestMethod.GET)
	public void downloadPDFReportTransaction(
			@RequestParam("username") String username,
			@RequestParam("cmbRole") String role,
			@RequestParam("datefrom") String dateFrom,
			@RequestParam("dateto") String dateTo,
			HttpServletResponse response, HttpServletRequest request) {

		List<SystemTimeUsed> listAll = sysTimeSer.searchAdvancedBlogon(
				username, role, dateTo, dateFrom);
		List<BlogLogonBean> listBlogLogon = sysTimeSer
				.parseToBlogLogon(listAll);
		String orignalFileName = "ReportBlogLogon.pdf";

		try {
			Document document = new Document();
			response.setHeader("Content-Disposition", "outline;filename=\""
					+ orignalFileName + "\"");
			PdfWriter.getInstance(document, response.getOutputStream());

			// image
			String realPath = request.getSession().getServletContext()
					.getRealPath("resources/main/img/logo.png");
			Image image = Image.getInstance(realPath);

			document.open();
			document.add(sysTimeSer.createTableBlogLogon(listBlogLogon, image));
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}